﻿Public Class AdministrarAlumnos



    Private Sub AdministrarAlumnos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cargarCombo(cmb_TipoDNI, "TipoDocumento", "idTipo", "nombre")
        cargarCombo(cmbRol, "Rol", "idRol", "nombre")
        Me.cmb_TipoDNI.SelectedIndex = -1
        Me.cmbRol.SelectedIndex = -1
        refrescar_grilla()
    End Sub

    Private Sub Btn_Salir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Salir.Click
        If MessageBox.Show("¿Está seguro que desea SALIR ?", "Importante", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Function insertarLocal() As Boolean
        Dim conex As New Conexion
        Dim consulta As String = ""

        consulta = "INSERT INTO Persona ("
        consulta &= "idTipoDocumento, numeroDocumento, nombre, apellido, idRol, usuario, password ) "
        consulta &= "values (" & Me.cmb_TipoDNI.SelectedValue & ", '" & Me.txt_NumeroDNI.Text & "', '"
        consulta &= Me.txt_Nombre.Text & "', '" & Me.txt_Apellido.Text & "', "
        consulta &= "" & Me.cmbRol.SelectedValue & ","
        consulta &= " '" & Me.txt_Usuario.Text & "', '" & Me.txtPass.Text & "')"

        conex._insertar(consulta)

        Return True
    End Function
    Public Sub refrescar_grilla()

        Dim tabla2 As New Data.DataTable
        Dim conex As New Conexion
        tabla2 = ejecutarConsulta("SELECT Persona.nombre, Persona.apellido, Persona.numeroDocumento, Persona.usuario FROM Persona")
        Me.DataGridView1.DataSource = tabla2

    End Sub
    Private Sub limpiar_grilla()

        Me.txt_Nombre.Text = ""
        Me.txt_Apellido.Text = ""
        Me.txt_NumeroDNI.Text = ""
        Me.txt_Usuario.Text = ""
        Me.txtPass.Text = ""
        Me.cmb_TipoDNI.SelectedIndex = -1
        Me.cmbRol.SelectedIndex = -1

    End Sub

    Private Sub modificar(ByVal idPersona As String)

        Dim conex As New Conexion
        Dim consulta As String = ""
        consulta = "UPDATE Persona "
        consulta &= "set nombre = '" & Me.txt_Nombre.Text & "'"
        consulta &= ", apellido = '" & Me.txt_Apellido.Text & "'"
        consulta &= ", idTipoDocumento = '" & Me.cmb_TipoDNI.SelectedValue
        consulta &= ", numeroDocumento = '" & Me.txt_NumeroDNI.Text & "'"
        consulta &= ", idRol = " & Me.cmbRol.SelectedValue
        consulta &= ", usuario = '" & Me.txt_Usuario.Text & "'"
        consulta &= ", password = '" & Me.txtPass.Text & "'"
        consulta &= " where idPersona = " & idPersona

        conex._modicar(consulta)
    End Sub

    Private Sub recupero(ByVal idPersona As String)
        Dim conex As New Conexion
        Dim dt As New Data.DataTable
        dt = conex._consulta("select * from Persona where idPersona = " & lbl_idPersona.Text)
        Me.txt_Nombre.Text = dt.Rows(0)("nombre")
        Me.txt_Apellido.Text = dt.Rows(0)("apellido")
        Me.txt_NumeroDNI.Text = dt.Rows(0)("numeroDocumento")
        Me.txtPass.Text = dt.Rows(0)("password")
        Me.txt_Usuario.Text = dt.Rows(0)("usuario")
        Me.cmbRol.SelectedValue = dt.Rows(0)("idRol")
        Me.cmb_TipoDNI.SelectedValue = dt.Rows(0)("idTipoDocumento")


    End Sub
    Private Sub borrar()
        Dim conex As New Conexion
        conex._borrar("DELETE FROM Persona WHERE idPersona =" & Me.lbl_idPersona.Text)
    End Sub

    Private Sub btn_Aceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Aceptar.Click


        Me.refrescar_grilla()



        If Me.txt_Nombre.Text = "" Then
            MessageBox.Show("NOMBRE ESTA VACIO...COMPLETELO!", "CUIDADO", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.txt_Nombre.Focus()
            Exit Sub
        End If

        If Me.txt_Apellido.Text = "" Then
            MessageBox.Show("APELLIDO ESTA VACIO...COMPLETELO!", "CUIDADO", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.txt_Apellido.Focus()
            Exit Sub
        End If

        If Me.txt_NumeroDNI.Text = "" Then
            MessageBox.Show("NUMERO DE DNI ESTA VACIO...COMPLETELO!", "CUIDADO", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.txt_NumeroDNI.Focus()
            Exit Sub
        End If

        If Me.txt_Usuario.Text = "" Then
            MessageBox.Show("USUARIO ESTA VACIO...COMPLETELO!", "CUIDADO", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.txt_Usuario.Focus()
            Exit Sub
        End If

        If Me.txtPass.Text = "" Then
            MessageBox.Show("PASSWORd ESTA VACIO...COMPLETELO!", "CUIDADO", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.txtPass.Focus()
            Exit Sub
        End If

        If Me.cmbRol.Text = "" Then
            MessageBox.Show("ROL NO FUE SELECCIONADO...COMPLETELO!", "CUIDADO", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.cmbRol.Focus()
            Exit Sub
        End If

        If Me.cmb_TipoDNI.Text = "" Then
            MessageBox.Show("TIPO DE DNI NO FUE SELECCIONADO...COMPLETELO!", "CUIDADO", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.cmb_TipoDNI.Focus()
            Exit Sub
        End If

        

        Try
            insertarLocal()
        Catch ex As Exception

        End Try

        Me.limpiar_grilla()
        Me.refrescar_grilla()

    End Sub

    Private Sub btn_Eliminar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Eliminar.Click
        If Me.DataGridView1.RowCount > 1 Then
            If Me.DataGridView1.SelectedRows.Count > 0 Then
                If MessageBox.Show("Está seguro que quiere borra ese ALUMNO?", "Importante", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
                    Me.borrar()
                    MessageBox.Show("ALUMNO BORRADO CON EXITO", "Atencion!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                    Me.refrescar_grilla()
                End If
            Else
                MessageBox.Show("DEBE SELECCIONAR LA FILA A ELIMINAR", "Atencion!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                Exit Sub
            End If
        Else
            MsgBox("La Tabla no contiene ningun ALUMNO", MsgBoxStyle.Critical, "Importante")
            Exit Sub
        End If
    End Sub

    Private Sub DataGridView1_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellChanged
        Try
            Me.lbl_idPersona.Text = CStr(Me.DataGridView1.Item(0, Me.DataGridView1.CurrentRow.Index).Value)

        Catch ex As Exception
        End Try
    End Sub

    Private Sub DataGridView1_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
        Me.recupero(lbl_idPersona.Text)
    End Sub

    Private Sub btn_Modificar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Modificar.Click
        Me.modificar((Me.lbl_idPersona.Text))
        MessageBox.Show("Se modifico el ALUMNO exitosamente", "Importante", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Me.limpiar_grilla()
        Me.refrescar_grilla()
    End Sub
End Class